﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using DataGenerator.DefaultSets;
using Repository;

namespace DataGenerator
{
    /// <summary>
    /// Main class.
    /// </summary>
    public class Program
    {
        /// <summary>
        /// Random number generator.
        /// </summary>
        private static readonly Random RandGen = new Random();

        public static void Main(string[] args)
        {
            using (var context = new ModelContainer())
            {
                var roleSet = new RoleSet(context.RoleSet);
                context.RoleSet.AddObject(roleSet.Guest);
                var user1 = new User { Name = "admin", PasswordHash = "admin", Role = roleSet.Administrator};
                var user2 = new User { Name = "samal", PasswordHash = "samal", Role = roleSet.User};
                context.UserSet.AddObject(user1);
                context.UserSet.AddObject(user2);

                var eventGroupTypeSet = new EventGroupTypeSet(context.EventGroupTypeSet);
                var personGroupTypeSet = new PersonGroupTypeSet(context.PersonGroupTypeSet);
                var placeGroupTypeSet = new PlaceTypeSet(context.PlaceTypeSet);

                var group = new PersonGroup {Name = "Кафедра ЭВМ", PersonGroupType = personGroupTypeSet.Department};
                var location = new Location {Name = "Корпус 5", Address = "Гикало"};

                var auditory1 = new Place {Name = "501"};
                var auditory2 = new Place {Name = "502"};
                var auditory3 = new Place {Name = "503"};
                var auditory4 = new Place {Name = "505"};
                var auditory5 = new Place {Name = "506"};
                var auditory6 = new Place {Name = "508"};
                var auditory7 = new Place {Name = "509"};
                var auditory8 = new Place {Name = "512"};
                var auditory9 = new Place {Name = "514"};

                var auditoryList = new List<Place>
                                       {
                                           auditory1,
                                           auditory2,
                                           auditory3,
                                           auditory4,
                                           auditory5,
                                           auditory6,
                                           auditory7,
                                           auditory8,
                                           auditory9,
                                       };

                foreach (var place in auditoryList)
                {
                    place.ResponsiblePersonGroup.Add(group);
                    place.Location = location;
                    place.PlaceType = placeGroupTypeSet.Laboratory;
                }


                var teachers = GetEvmDepartment();
                teachers.Find(p=>p.FirstName == "Самаль").User = user2;
                //var studyCourses = CreateStudyCourses(30, eventGroupTypeSet.StudyCourse, "StydyCourse");

                group.Person.AddRange(teachers);

                var eventGroup1 = new EventGroup { Name = "Лекция", Description = "лк", EventGroupType = eventGroupTypeSet.Class };
                var eventGroup2 = new EventGroup { Name = "Практическое занятие", Description = "пз", EventGroupType = eventGroupTypeSet.Class };
                var eventGroup3 = new EventGroup { Name = "Лабораторная работа", Description = "лр", EventGroupType = eventGroupTypeSet.Class };
                var eventGroup4 = new EventGroup { Name = "Консультация", Description = "к", EventGroupType = eventGroupTypeSet.Class };
                var eventGroup5 = new EventGroup { Name = "Зачёт", Description = "зч", EventGroupType = eventGroupTypeSet.Class };
                var eventGroup6 = new EventGroup { Name = "Экзамен", Description = "экз", EventGroupType = eventGroupTypeSet.Class };
                var eventGroup7 = new EventGroup { Name = "Семинар", Description = "см", EventGroupType = eventGroupTypeSet.Class };
                var eventGroup8 = new EventGroup { Name = "Заседание", Description = "зс", EventGroupType = eventGroupTypeSet.Class };
                var eventGroup9 = new EventGroup { Name = "Дополнительное занятие", Description = "дз", EventGroupType = eventGroupTypeSet.Class };
                
                var eventGroup10 = new EventGroup { Name = "1-я неделя", EventGroupType = eventGroupTypeSet.Periodic };
                var eventGroup11 = new EventGroup { Name = "2-я неделя", EventGroupType = eventGroupTypeSet.Periodic };
                var eventGroup12 = new EventGroup { Name = "3-я неделя", EventGroupType = eventGroupTypeSet.Periodic };
                var eventGroup13 = new EventGroup { Name = "4-я неделя", EventGroupType = eventGroupTypeSet.Periodic };

                var periodicGroups = new List<EventGroup>
                                         {
                                             eventGroup10,
                                             eventGroup11,
                                             eventGroup12,
                                             eventGroup13,
                                         };

                var classesTypes = new List<EventGroup>
                                       {
                                          eventGroup1,
                                          eventGroup2,
                                          eventGroup3,
                                          eventGroup4,
                                          eventGroup5,
                                          eventGroup6,
                                          eventGroup7,
                                          eventGroup8,
                                          eventGroup9,
                                       };



                //var from = new DateTime(2011, 9, 1);
                //var to = new DateTime(2012, 1, 1);
                //var events = CreateEvents(from, to, classesTypes, studyCourses, periodicity);

                var j = 0;
                var users = new List<User>();
                var organizePersonGroups = new List<PersonGroup>();
                foreach (var teacher in teachers)
                {
                    var personGroup = new PersonGroup {Name = teacher.FirstName + " group"};
                    personGroup.Person.Add(teacher);
                    personGroup.PersonGroupType = personGroupTypeSet.SinglePerson;
                    organizePersonGroups.Add(personGroup);

                    var user = new User
                                   {
                                       Name = "user" + j++,
                                       PasswordHash = "user",
                                       Role = roleSet.User,
                                   };
                    users.Add(user);
                }

                users.Add(new User{Name = "admin", PasswordHash = "admin", Role = roleSet.Administrator});

                context.UserSet.AddObjects(users);
                context.EventGroupSet.AddObjects(classesTypes);
                context.EventGroupSet.AddObjects(periodicGroups);
                context.PersonSet.AddObjects(teachers);
                context.PlaceSet.AddObjects(auditoryList);
/*
                for (var i = 0; i < events.Count; i++)
                {
                    var @event = events[i];
                    @event.Place =
                        auditoryList.Where(p =>
                                           p.Event.FirstOrDefault(e => e.StartTime == @event.StartTime) == null).
                            GetRandomItem();

                    if (@event.Place == null)
                    {
                        
                        continue;
                        Console.WriteLine("No free auditory");
                    }

                    @event.OrganizePersonGroup = organizePersonGroups.GetRandomItem();
                    @event.Name = "Занятие проводит " + @event.OrganizePersonGroup.Name;

                    if (RandGen.Next(5) == 1 &&
                        @event.EventGroup.FirstOrDefault(g => g == eventGroup1) != null)
                    {
                        // @event.ParticipatePersonGroup = streamList.GetRandomItem();
                    }
                    else
                    {
                        // @event.ParticipatePersonGroup = studyGroupList.GetRandomItem();
                    }
                }
                context.PlaceSet.AddObjects(auditoryList);

                foreach (var @event in events)
                {
                    if (@event.Place == null)
                    {
                        context.EventSet.DeleteObject(@event);
                    }
                }
 * */
                context.SaveChanges();
            }
        }


        private static List<Person> GetEvmDepartment()
        {

            var person1 = new Person
            {
                FirstName = "Лукьянова",
                LastName = "Ирина",
                Patronymic = "Владимировна"
            };

            var person2 = new Person
            {
                FirstName = "Поттосин",
                LastName = "Юрий",
                Patronymic = "Васильевич"
            };


            var person3 = new Person
            {
                FirstName = "Сасин",
                LastName = "Евгений",
                Patronymic = "Александрович"
            };


            var person4 = new Person
            {
                FirstName = "Галкин",
                LastName = "Василий",
                Patronymic = "Иванович"
            };


            var person5 = new Person
            {
                FirstName = "Одинец",
                LastName = "Дмитрий",
                Patronymic = "Николаевич"
            };


            var person6 = new Person
            {
                FirstName = "Козловский",
                LastName = "А.",
                Patronymic = "Н."
            };


            var person7 = new Person
            {
                FirstName = "Кобяк",
                LastName = "Игорь",
                Patronymic = "Петрович"
            };


            var person8 = new Person
            {
                FirstName = "Петровский",
                LastName = "Алексей",
                Patronymic = "Алексеевич"
            };


            var person9 = new Person
            {
                FirstName = "Пынькин",
                LastName = "Денис",
                Patronymic = "Александрович"
            };


            var person10 = new Person
            {
                FirstName = "Клевакин",
                LastName = "Максим",
                Patronymic = "Дмитриевич"
            };


            var person11 = new Person
            {
                FirstName = "Перцев",
                LastName = "Дмитрий",
                Patronymic = "Юрьевич"
            };


            var person12 = new Person
            {
                FirstName = "Кавальчук",
                LastName = "Александр",
                Patronymic = "Николаевич"
            };


            var person13 = new Person
            {
                FirstName = "Глецевич",
                LastName = "Иван",
                Patronymic = "Иванович"
            };


            var person14 = new Person
            {
                FirstName = "Самаль",
                LastName = "Дмитрий",
                Patronymic = "Иваноович"
            };

            var person15 = new Person
            {
                FirstName = "Лукашевич",
                LastName = "Марина",
                Patronymic = "Михайловна"
            };

            var person16 = new Person
            {
                FirstName = "Гуз",
                LastName = "Геннадий",
                Patronymic = "Андреевич"
            };

            var person17 = new Person
            {
                FirstName = "Калабухов",
                LastName = "Евгений",
                Patronymic = "Валерьевич"
            };

            var person18 = new Person
            {
                FirstName = "Ковальчук",
                LastName = "Анна",
                Patronymic = "Михайловна"
            };

            var person19 = new Person
            {
                FirstName = "Отвагин",
                LastName = "Алексей",
                Patronymic = "Владимирович"
            };

            var person20 = new Person
            {
                FirstName = "Супонев",
                LastName = "Виктор",
                Patronymic = "Алексеевич"
            };

            var person21 = new Person
            {
                FirstName = "Авсеев",
                LastName = "Алексей",
                Patronymic = "Викторович"
            };

            var person22 = new Person
            {
                FirstName = "Самусевич",
                LastName = "Александр",
                Patronymic = "Викторович"
            };

            var person23 = new Person
            {
                FirstName = "Дорошев",
                LastName = "Александр",
                Patronymic = "Алексеевич"
            }; 
            
            var person24 = new Person
            {
                FirstName = "Прытков",
                LastName = "Валерий",
                Patronymic = "Александрович"
            };

            var person25 = new Person
            {
                FirstName = "Липская",
                LastName = "Елена",
                Patronymic = "Владимировна"
            };

            var person26 = new Person
            {
                FirstName = "Волох",
                LastName = "Олег",
                Patronymic = "Алексеевич"
            };

            var person27 = new Person
            {
                FirstName = "Сидорович",
                LastName = "Александра",
                Patronymic = "Сергеевна"
            };

            var person28 = new Person
            {
                FirstName = "Клипцевич",
                LastName = "Евгений",
                Patronymic = "Евгеньевич"
            };

            var person29 = new Person
            {
                FirstName = "Матросов",
                LastName = "Алексей",
                Patronymic = "Алексеевич"
            };

            var person30 = new Person
            {
                FirstName = "Иванов",
                LastName = "Иван",
                Patronymic = "Иванович"
            };

            var person31 = new Person
            {
                FirstName = "Луцик",
                LastName = "Юрий",
                Patronymic = "Александрович"
            };

            var person32 = new Person
            {
                FirstName = "Уваров",
                LastName = "Андрей",
                Patronymic = "Александрович"
            };

            var person33 = new Person
            {
                FirstName = "Садыхов",
                LastName = "Рауф",
                Patronymic = "Хосровович"
            };

            var person34 = new Person
            {
                FirstName = "Демидчук",
                LastName = "Алексей",
                Patronymic = "Николаевич"
            };

            var person35 = new Person
            {
                FirstName = "Байрак",
                LastName = "Сергей",
                Patronymic = "Анатольевич"
            };

            var person36 = new Person
            {
                FirstName = "Тимошенко",
                LastName = "Николай",
                Patronymic = "Степанович"
            };

            var person37 = new Person
            {
                FirstName = "Козловский",
                LastName = "Антон",
                Patronymic = "Николаевич"
            };

            var person38 = new Person
            {
                FirstName = "Фролов",
                LastName = "Игорь",
                Patronymic = "Николаевич"
            };

            var person39 = new Person
            {
                FirstName = "Татур",
                LastName = "Михаил",
                Patronymic = "Михайлович"
            };

            var person40 = new Person
            {
                FirstName = "Искра",
                LastName = "Наталья",
                Patronymic = "Александровна"
            };

            var persons = new List<Person>
                              {
                                  person1,
                                  person2,
                                  person3,
                                  person4,
                                  person5,
                                  person6,
                                  person7,
                                  person8,
                                  person9,
                                  person10,
                                  person11,
                                  person12,
                                  person13,
                                  person14,
                                  person15,
                                  person16,
                                  person17,
                                  person18,
                                  person19,
                                  person20,
                                  person21,
                                  person22,
                                  person23,
                                  person24,
                                  person25,
                                  person26,
                                  person27,
                                  person28,
                                  person29,
                                  person30,
                                  person31,
                                  person32,
                                  person33,
                                  person34,
                                  person35,
                                  person36,
                                  person37,
                                  person38,
                                  person39,
                                  person40,
                              };

            var c = StringComparer.Create(CultureInfo.CurrentCulture, true);
            persons.Sort((x, y) => c.Compare(x.FirstName, y.FirstName));
            return persons;
        }


        private DateTime GetBirthday()
        {
            return DateTime.Now.AddDays(-(20 + RandGen.Next(60)));
        }

        /// <summary>
        /// Mains the specified args.
        /// </summary>
        /// <param name="args">The args.</param>
        public static void Main1(string[] args)
        {
            using (var context = new ModelContainer())
            {
                if (context.DatabaseExists())
                {
                    context.DeleteDatabase();
                }

                context.CreateDatabase();

                var personGroupTypeSet = new PersonGroupTypeSet(context.PersonGroupTypeSet);
                var eventGroupTypeSet = new EventGroupTypeSet(context.EventGroupTypeSet);
                var placeGroupTypeSet = new PlaceTypeSet(context.PlaceTypeSet);
                var equipmentTypeSet = new EquipmentTypeSet(context.EquipmentTypeSet);
                var locationSet = new LocationSet(context.LocationSet);

                var university = new PersonGroup {PersonGroupType = personGroupTypeSet.University, Name = "BSUIR"};
                var facultyList = CreatePersonGroups(3, personGroupTypeSet.Faculty);
                var streamList = CreatePersonGroups(50, personGroupTypeSet.Stream);
                var departmentList = CreatePersonGroups(20, personGroupTypeSet.Department);
                var specialityList = CreatePersonGroups(10, personGroupTypeSet.Speciality);
                var studyGroupList = CreatePersonGroups(250, personGroupTypeSet.StydyGroup);
                
                // var studySubgroupList = CreateGroups(300, groupTypeList.StydySubgroup);
                var studentGroup = new PersonGroup
                                       {Name = "Students", PersonGroupType = personGroupTypeSet.PersonsCollaboration};
                var employeeGroup = new PersonGroup
                                        {Name = "Employees", PersonGroupType = personGroupTypeSet.PersonsCollaboration};

                context.PersonGroupSet.AddObject(university);

                // факультеты университета
                SetHierarchy(university, facultyList);

                // кафедры университета
                SetHierarchy(university, departmentList);

                // специальности факультета
                SetHierarchy(facultyList[0], specialityList.GetRange(0, 4));
                SetHierarchy(facultyList[1], specialityList.GetRange(4, 3));
                SetHierarchy(facultyList[2], specialityList.GetRange(7, 3));

                // кафедры факультета
                SetHierarchy(facultyList[0], departmentList.GetRange(0, 7));
                SetHierarchy(facultyList[1], departmentList.GetRange(7, 7));
                SetHierarchy(facultyList[2], departmentList.GetRange(14, 6));

                // потоки кафедры
                for (var i = 0; i < departmentList.Count; i++)
                {
                    SetHierarchy(departmentList[i], streamList.GetRange(2*i, 2));
                }

                // кафедры университета
                for (var i = 0; i < specialityList.Count; i++)
                {
                    SetHierarchy(specialityList[i], streamList.GetRange(5*i, 5));
                }

                // кафедры университета
                for (var i = 0; i < streamList.Count; i++)
                {
                    SetHierarchy(streamList[i], studyGroupList.GetRange(5*i, 5));
                }

                // группы потока
                for (var i = 0; i < streamList.Count; i++)
                {
                    SetHierarchy(streamList[i], studyGroupList.GetRange(5*i, 5));
                }

                foreach (var studyGroup in studyGroupList)
                {
                    var students = CreatePersons(20, "Student");
                    AddPersonsToPersonGroup(students, studyGroup);
                    AddPersonsToPersonGroup(students, studentGroup);
                }

                var employees = CreatePersons(100, "Employee");
                var teachers = CreatePersons(50, "Teacher");
                AddPersonsToPersonGroup(employees, employeeGroup);
                AddPersonsToPersonGroup(teachers, employeeGroup);

                // context.SaveChanges();
                // -------------------------
                var places = new List<Place>();
                places.AddRange(CreatePlaces(60, locationSet.Location1, placeGroupTypeSet));
                places.AddRange(CreatePlaces(30, locationSet.Location2, placeGroupTypeSet));
                places.AddRange(CreatePlaces(50, locationSet.Location3, placeGroupTypeSet));
                places.AddRange(CreatePlaces(40, locationSet.Location4, placeGroupTypeSet));
                places.AddRange(CreatePlaces(80, locationSet.Location5, placeGroupTypeSet));

                var equipments = new List<Equipment>();

                foreach (var place in places)
                {
                    var board = context.EquipmentSet.CreateObject();
                    board.Name = "Board-" + place.Name;
                    board.EquipmentType = equipmentTypeSet.Board;
                    place.Equipment.Add(board);
                    if (place.PlaceType == placeGroupTypeSet.Laboratory)
                    {
                        var items = CreateEquipment(RandGen.Next(10), equipmentTypeSet.LaboratoryStand,
                                                    "Stand-" + place.Name + "-");
                        equipments.AddRange(items);
                        place.Equipment.AddRange(items);
                    }

                    if (place.PlaceType == placeGroupTypeSet.LectureAuditory)
                    {
                        var items = CreateEquipment(RandGen.Next(1), equipmentTypeSet.Projector,
                                                    "Projector-" + place.Name + "-");
                        equipments.AddRange(items);
                        place.Equipment.AddRange(items);
                    }

                    if (place.PlaceType == placeGroupTypeSet.Auditory)
                    {
                        var items = CreateEquipment(RandGen.Next(10), equipmentTypeSet.Screen,
                                                    "Screen-" + place.Name + "-");
                        equipments.AddRange(items);
                        place.Equipment.AddRange(items);
                    }
                }
                                   
                // context.SaveChanges();
                // -------------------------
                var studyCourses = CreateStudyCourses(30, eventGroupTypeSet.StudyCourse, "StydyCourse");
                var lectureEventGroup = new EventGroup {Name = "Lecture", EventGroupType = eventGroupTypeSet.Class};
                var seminarEventGroup = new EventGroup {Name = "Seminar", EventGroupType = eventGroupTypeSet.Class};
                var practiceEventGroup = new EventGroup {Name = "Practice", EventGroupType = eventGroupTypeSet.Class};
                var laboratoryEventGroup = new EventGroup
                                               {Name = "Laboratory", EventGroupType = eventGroupTypeSet.Class};

                var classesTypes = new List<EventGroup>
                                       {
                                           lectureEventGroup,
                                           seminarEventGroup,
                                           practiceEventGroup,
                                           laboratoryEventGroup,
                                           practiceEventGroup
                                       };

                var single = new EventGroup {Name = "Single", EventGroupType = eventGroupTypeSet.Periodic};
                var periodic = new EventGroup {Name = "Periodic", EventGroupType = eventGroupTypeSet.Periodic};

                var periodicity = new List<EventGroup> {single, periodic};

                var from = new DateTime(2011, 9, 1);
                var to = new DateTime(2012, 1, 1);
                var events = CreateEvents(from, to, classesTypes, studyCourses, periodicity);
                var organizePersonGroups = teachers.Select(teacher => new PersonGroup
                                                                          {
                                                                              Name =
                                                                                  teacher.FirstName + " " +
                                                                                  teacher.LastName,
                                                                              PersonGroupType =
                                                                                  personGroupTypeSet.
                                                                                  PersonsCollaboration
                                                                          }).ToList();

                for (var i = 0; i < events.Count; i++)
                {
                    var @event = events[i];
                    @event.OrganizePersonGroup = organizePersonGroups.GetRandomItem();

                    var teacher = teachers.GetRandomItem();
                    @event.OrganizePersonGroup.Person.Add(teacher);
                    @event.OrganizePersonGroup.Name = "Event" + i + @event.OrganizePersonGroup.Person.RelationshipName;

                    if (RandGen.Next(5) == 1 &&
                        @event.EventGroup.FirstOrDefault(g => g == lectureEventGroup) != null)
                    {
                        @event.ParticipatePersonGroup = streamList.GetRandomItem();
                    }
                    else
                    {
                        @event.ParticipatePersonGroup = studyGroupList.GetRandomItem();
                    }

                    @event.Place =
                        places.Where(p => p.Capacity >= @event.ParticipatePersonGroup.Person.Count &&
                                          p.Event.FirstOrDefault(e => e.StartTime == @event.StartTime) == null).
                            GetRandomItem();

                    if (@event.Place == null)
                    {
                        Console.WriteLine("No free auditory");
                    }
                }

                context.SaveChanges();
            }
        }

        /// <summary>
        /// Creates the equipment.
        /// </summary>
        /// <param name="count">The count.</param>
        /// <param name="equipmentType">Type of the equipment.</param>
        /// <param name="nameTemplate">The name template.</param>
        /// <returns>Equipment list.</returns>
        private static List<Equipment> CreateEquipment(int count, EquipmentType equipmentType, string nameTemplate = null)
        {
            var equipments = new List<Equipment>();
            for (var i = 0; i < count; i++)
            {
                var equipment = new Equipment
                {
                    Name = nameTemplate + i,
                    EquipmentType = equipmentType,
                };
                equipments.Add(equipment);
            }

            return equipments;
        }

        /// <summary>
        /// Creates the places.
        /// </summary>
        /// <param name="count">The count.</param>
        /// <param name="location">The location.</param>
        /// <param name="placeTypeSet">The place type set.</param>
        /// <returns>List of places.</returns>
        private static IEnumerable<Place> CreatePlaces(int count, Location location, PlaceTypeSet placeTypeSet)
        {
            var places = new List<Place>();
            for (var i = 0; i < count; i++)
            {
                PlaceType auditoryType;
                int capacity;
                switch (RandGen.Next(7))
                {
                    case 1:
                        auditoryType = placeTypeSet.ComputerClass;
                        capacity = 10 + RandGen.Next(20);
                        break;
                    case 2:
                        auditoryType = placeTypeSet.LectureAuditory;
                        capacity = 50 + RandGen.Next(100);
                        break;
                    case 3:
                        auditoryType = placeTypeSet.Laboratory;
                        capacity = 10 + RandGen.Next(20);
                        break;
                    default:
                        auditoryType = placeTypeSet.Auditory;
                        capacity = 10 + RandGen.Next(20);
                        break;
                }

                var place = new Place
                                {
                                    Name = i.ToString(CultureInfo.InvariantCulture),
                                    Location = location,
                                    PlaceType = auditoryType,
                                    Capacity = capacity
                                };
                places.Add(place);
            }

            return places;
        }

        /// <summary>
        /// Creates the events.
        /// </summary>
        /// <param name="from">From date.</param>
        /// <param name="to">To date.</param>
        /// <param name="classesTypes">The classes types.</param>
        /// <param name="studyCourses">The study courses.</param>
        /// <param name="periodicity">The periodicity.</param>
        /// <returns>List of events.</returns>
        private static List<Event> CreateEvents(DateTime from, DateTime to, List<EventGroup> classesTypes,
                                                List<EventGroup> studyCourses, List<EventGroup> periodicity)
        {
            var pairs = new List<TimeSpan>
                            {
                                new TimeSpan(8, 0, 0),
                                new TimeSpan(9, 45, 0),
                                new TimeSpan(11, 40, 0),
                                new TimeSpan(13, 25, 0),
                                new TimeSpan(15, 30, 0),
                                new TimeSpan(17, 05, 0),
                            };

            var events = new List<Event>();
            for (var i = from; i < to; i = i.AddDays(1))
            {
                foreach (var pair in pairs)
                {
                    var start = i.Date.Add(pair);
                    for (var j = 0; j < 20 + RandGen.Next(60); j++)
                    {
                        var e = new Event {StartTime = start, EndTime = start.AddHours(1.5)};

                        if (RandGen.Next(2) == 1)
                        {
                            events.Add(e);
                            var classesType = classesTypes.GetRandomItem();
                            e.EventGroup.Add(classesType);

                            var studyCourse = studyCourses.GetRandomItem();
                            e.EventGroup.Add(studyCourse);

                            e.EventGroup.Add(periodicity[1]);

                            e.Name = "Event";
                        }
                    }
                }
            }

            return events;
        }

        /// <summary>
        /// Creates the specified count.
        /// </summary>
        /// <param name="count">The count.</param>
        /// <param name="groupType">Type of the group.</param>
        /// <param name="nameTemplate">The name template.</param>
        /// <returns>New groups.</returns>
        private static List<PersonGroup> CreatePersonGroups(int count, PersonGroupType groupType,
                                                            string nameTemplate = null)
        {
            if (nameTemplate == null)
            {
                nameTemplate = groupType.Name;
            }

            var groups = new List<PersonGroup>();
            for (var i = 0; i < count; i++)
            {
                groups.Add(new PersonGroup {PersonGroupType = groupType, Name = nameTemplate + i});
            }

            return groups;
        }

        /// <summary>
        /// Sets the hierarchy.
        /// </summary>
        /// <param name="parent">The parent.</param>
        /// <param name="children">The children.</param>
        private static void SetHierarchy(PersonGroup parent, List<PersonGroup> children)
        {
            parent.ChildPersonGroup.AddRange(children);

            foreach (var child in children)
            {
                child.ParentPersonGroup.Add(parent);
            }
        }

        /// <summary>
        /// Creates the specified count.
        /// </summary>
        /// <param name="count">The count.</param>
        /// <param name="nameTemplate">The name template.</param>
        /// <returns>New groups.</returns>
        private static List<Person> CreatePersons(int count, string nameTemplate = null)
        {
            var age = RandGen.Next(30);
            var day = RandGen.Next(365);
            var persons = new List<Person>();
            for (var i = 0; i < count; i++)
            {
                var person = new Person
                                 {
                                     FirstName = nameTemplate + i,
                                     LastName = nameTemplate + i,
                                     Patronymic = nameTemplate + i,
                                     Birthday = DateTime.Now.AddYears(-age).AddDays(day),
                                 };
                persons.Add(person);
            }

            return persons;
        }

        /// <summary>
        /// Sets the persons to group.
        /// </summary>
        /// <param name="persons">The persons.</param>
        /// <param name="group">The group.</param>
        private static void AddPersonsToPersonGroup(IEnumerable<Person> persons, PersonGroup group)
        {
            foreach (var person in persons)
            {
                person.PersonGroup.Add(group);
            }
        }

        /// <summary>
        /// Creates the study courses.
        /// </summary>
        /// <param name="count">The count.</param>
        /// <param name="groupType">Type of the group.</param>
        /// <param name="nameTemplate">The name template.</param>
        /// <returns>List of event groups.</returns>
        private static List<EventGroup> CreateStudyCourses(int count, EventGroupType groupType, string nameTemplate = null)
        {
            if (nameTemplate == null)
            {
                nameTemplate = groupType.Name;
            }

            var groups = new List<EventGroup>();
            for (var i = 0; i < count; i++)
            {
                groups.Add(new EventGroup {EventGroupType = groupType, Name = nameTemplate + i});
            }

            return groups;
        }
    }
}
